<template>
	<view class="content">
		<image class="logo" src="/static/logo/logo.png"></image>
		<view class="text-area">
			<text class="title">选择微信聊天记录文件上传</text>
		</view>
		<button type="primary" @click="upload()">上传文件</button>
		<h3 style="margin-top: 10px;">文件列表</h3>
		<view class="title" v-for="(item,index) in fileList" style="margin-top: 10px;">
			{{item.name}}
		</view>
		<!-- 		
		<th-file-picker ref="filePick" :types="types" :showTitle="false" count="9" @result="result">
		</th-file-picker> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				fileList: [],
				types: ["file"],
				config: {},
				file_name: '请先上传',
			}
		},
		onLoad() {
			uni.uv.api.user.getUser(1).then(res => {
				this.user = res;
			})
			this.config = this.$config
		},
		methods: {

			// upload(){
			//     this.$refs.filePick.preUpload()
			// },

			// result(data){
			//     //TODO 处理上传结果
			// 	console.log(data);
			// },

			async upload() {
				let that = this
				uni.chooseMessageFile({
					count: 1,
					type:"file",
					success: function(res) {
						const tempFilePaths = res.tempFiles
						uni.showLoading({
							title: "上传中",
						})
						console.log("----------------------------------");
						console.log(tempFilePaths[0]);
						console.log("----------------------------------");

						// uni.uploadFile({
						//   url: 'https://hd215.api.yesapi.cn/?s=App.CDN.UploadOffice&return_data=0&app_key=2ECF8391699A35405A559F360A6E1482&sign=7A1A19021EFEA1A254BD94DB0A6E73E1',
						//   filePath: tempFilePaths[0].path,
						//   name: 'file',
						//   success: (res) => {
						//     console.log(res);
						//   },
						//   fail: (err) => {
						//     console.log(err);
						//   }
						// })

						uni.getFileSystemManager().readFile({
							filePath: tempFilePaths[0].path,
							encoding: 'base64',
							success(res) {
								// console.log(res.data)
								const options = {
									// url: 'http://api.yesapi.cn/api/App/CDN/UploadOfficeByBase64',
									// url: 'https://hd215.api.yesapi.cn/?s=App.CDN.UploadOfficeByBase64&return_data=0&app_key=2ECF8391699A35405A559F360A6E1482&sign=971FCFD0D37B406AFDAA2773206A5A3D',
									url: 'https://hn216.api.yesapi.cn/?s=App.CDN.UploadOfficeByBase64&return_data=0&app_key=5F73F69E80178B24DF82D579A15C5BA2&sign=2A69FCE7E77EFF453322EF750BB4D7A7',
									method: 'POST',
									data: {
										file: res.data,
										file_name: tempFilePaths[0]['name']
									}
								}

								console.log("----------------------------------");
								console.log(options);
								console.log("----------------------------------");

								uni.request({
									url: options.url,
									method: options.method,
									data: options.data,
									success(reqRes) {
										console.log("成功", reqRes);
										if (reqRes.data.data.err_code == 0) {
											that.fileList.push({
												name: tempFilePaths[0]['name']
											})
											uni.showToast({
												title: '上传成功',
												icon: "none",
												position: 'bottom'
											});
											uni.hideLoading()
										}else{
											uni.showToast({
												title: '上传失败',
												icon: "none",
												position: 'bottom'
											});
											uni.hideLoading()
										}
									}
								})
							},

							fail(res) {
								console.error(res)
							},
						})

					}
				})

			},

		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 400rpx;
		width: 400rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
		margin-bottom: 10px;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>